﻿using System;
using System.Collections;

namespace SSISComponents
{
    public class ColumnCollection : System.Collections.CollectionBase
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public Column Item(int index)
        {
            return (Column)List[index];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        public void Remove(int index)
        {
            if (index > Count - 1 | index < 0)
            {
                throw new Exception("Index not valid!");
            }
            else
            {
                List.RemoveAt(index);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Column"></param>
        public void Remove(Column Column)
        {
            int index = 0;
            foreach (Column Item in List)
            {
                if (Item.Name == Column.Name)
                {
                   break;
                }
                index += 1;
            }
            List.RemoveAt(index);
        }

        /// <summary>
        /// Removes all Items where property remove is set to true
        /// </summary>
        public void Clean()
        {
            ArrayList ItemIndex = new ArrayList();
            foreach (Column Item in List)
            {
                if (Item.Remove == true)
                {
                    ItemIndex.Add(Item);
                }
             }

            for (int k = 0; k < ItemIndex.Count; k++)
			{
                List.Remove(ItemIndex[k]);
			}
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Col"></param>
        public void Add(Column Col)
        {
            List.Add(Col);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="DataType"></param>
        /// <param name="Length"></param>
        /// <param name="Precision"></param>
        /// <param name="Scale"></param>
        /// <param name="Codepage"></param>
        public void Add(string Name, Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType DataType, int Length, int Precision, int Scale, int Codepage)
        {
            Column Co = new Column();
            Co.Name = Name;
            Co.DataType = DataType;
            Co.Length = Length;
            Co.Codepage = Codepage;
            Co.Precision = Precision;
            Co.Scale = Scale;
            List.Add(Co);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="DataType"></param>
        /// <param name="Length"></param>
        /// <param name="Precision"></param>
        /// <param name="Scale"></param>
        /// <param name="Codepage"></param>
        /// <param name="Description"></param>
        public void Add(string Name, Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType DataType, int Length, int Precision, int Scale, int Codepage, string Description)
        {
            Column Co = new Column();
            Co.Name = Name;
            Co.DataType = DataType;
            Co.Length = Length;
            Co.Codepage = Codepage;
            Co.Precision = Precision;
            Co.Scale = Scale;
            Co.Description = Description;
            List.Add(Co);
        }

        /// <summary>
        /// Adds an element to the collection
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="DataType"></param>
        /// <param name="Length"></param>
        /// <param name="Precision"></param>
        /// <param name="Scale"></param>
        /// <param name="Codepage"></param>
        /// <param name="Description"></param>
        /// <param name="LineageID"></param>
        public void Add(string Name, Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType DataType, int Length, int Precision, int Scale, int Codepage, string Description, int LineageID)
        {
            Column Co = new Column();
            Co.Name = Name;
            Co.DataType = DataType;
            Co.Length = Length;
            Co.Codepage = Codepage;
            Co.Precision = Precision;
            Co.Scale = Scale;
            Co.Description = Description;
            Co.LineageID = LineageID;
            List.Add(Co);
        }

        /// <summary>
        /// Searches for the specified object and returns the index of the first occurence within the range of elements.
        /// </summary>
        /// <param name="Name">Name of Item</param>
        /// <returns></returns>
        public int IndexOf(string Name)
        {
            int RetIndex = -1;
            int Index = 0;
            foreach (Column Item in List)
            {
                if (Item.Name == Name)
                {
                    RetIndex = Index;
                    break;
                }
                Index += 1;
            }
            return RetIndex;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
                
        public Column GetColumnByName(string Name)
        {
            Column RetCol = null;
            foreach (Column Item in List)
            {
                if (Item.Name == Name)
                {
                    RetCol = Item;
                    break;
                }
            }
            return RetCol;
        }

    }
}
